package code.starivan.leetcode;

/**
 * Created by Ivan on 2015/8/22.
 */

/*Reverse a singly linked list.*/


public class N206_ReverseLinked {
    //Definition for singly-linked list.
    class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    public ListNode reverseList(ListNode head) {
        if(head==null)
            return head;

       ListNode pre=null;
        ListNode node=head;
        ListNode next=null;

        while (node!=null){
            next=node.next;
            node.next=pre;
            pre=node;
            node=next;
        }

        return pre;
    }
}
